package com.qf.service.impl;

import com.qf.dao.MailVerificationDao;
import com.qf.dao.TbUserDao;
import com.qf.pojo.MailVerification;
import com.qf.pojo.TbUser;
import com.qf.pojo.req.TbUserReq;
import com.qf.pojo.resp.BaseResult;
import com.qf.service.TbUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Random;

@Service
public class TbUserServiceImpl implements TbUserService {
    BaseResult baseResult = new BaseResult();
    @Resource
    TbUserDao tbUserDao;
    @Resource
    MailVerificationDao mailVerificationDao;

    @Autowired
    JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;
    @Override
    public BaseResult login(TbUser tbUser, HttpSession session) {
        System.out.println("请求登录用户："+tbUser);
        TbUser tbUser1 = tbUserDao.findByUserName(tbUser.getUserName());
        if (tbUser1!=null) {
            if (tbUser1.getPassword().equals(tbUser.getPassword())) {
                session.setAttribute("user", tbUser1);
                baseResult.setCode(1);
                baseResult.setInfo("登录成功");
                baseResult.setData(tbUser1);
                return baseResult;
            } else {
                baseResult.setCode(0);
                baseResult.setInfo("输入的密码有误，请确认后输入！");
                return baseResult;
            }
        }else {
                baseResult.setCode(0);
                baseResult.setInfo("输入的用户名有误，请重新输入！");
                return baseResult;
            }
    }

    @Override
    public BaseResult sendMail(String userName) {
        System.out.println("邮件发送地址："+userName);
        TbUser byUserName = tbUserDao.findByUserName(userName);
        if (byUserName==null){
            //生成一个随机验证码
            Random random = new Random();
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i <4 ; i++) {
                stringBuffer.append(random.nextInt(10));
            }

            try {
                //邮件发送
                SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
                simpleMailMessage.setTo(userName);
                simpleMailMessage.setFrom(from);
                simpleMailMessage.setSubject("注册验证码");
                simpleMailMessage.setText(stringBuffer.toString());
                javaMailSender.send(simpleMailMessage);

                //如果发送成功  则存入数据库
                MailVerification mailVerification = new MailVerification();
                mailVerification.setCode(stringBuffer.toString());
                mailVerification.setStatus(0);
                mailVerification.setCreatTime(new Date());
                mailVerification.setUserName(userName);

                Integer i = mailVerificationDao.insertMail(mailVerification);
                baseResult.setCode(1);
                baseResult.setInfo("邮件发送成功！");
                return baseResult;
            }catch (Exception e){
                e.printStackTrace();
                baseResult.setCode(0);
                baseResult.setInfo("邮件发送失败！");
                return baseResult;
            }

        }
        baseResult.setCode(0);
        baseResult.setInfo("用户名已存在，请登录");
        return baseResult;
    }


    @Override
    public BaseResult addUser(TbUserReq tbUserReq) {
        TbUser userName = tbUserDao.findByUserName(tbUserReq.getUserName());
        if (userName!=null){
            baseResult.setCode(0);
            baseResult.setInfo("用户名已存在，请更换");
            return baseResult;
        }
        //判断验证码
        MailVerification mailVerification = mailVerificationDao.findByUserNameAndCode(tbUserReq.getUserName(),tbUserReq.getCode());
        //进行验证码的比对

        if (mailVerification==null){
            baseResult.setCode(0);
            baseResult.setInfo("验证码错误1");
            return baseResult;
        }
        if ( !mailVerification.getCode().equals(tbUserReq.getCode())){
            baseResult.setCode(0);
            baseResult.setInfo("验证码错误2");
            return baseResult;
        }

        //增加到数据库  将请求对象转为数据库存储对象
        TbUser user = new TbUser();
        BeanUtils.copyProperties(tbUserReq,user);
        user.setCreatTime(new Date());
        Integer  i= tbUserDao.addUser(user);
        baseResult.setCode(1);
        baseResult.setInfo("注册成功");
        return baseResult;

    }


}
